﻿using Newtonsoft.Json;
using System;
using System.Diagnostics;
using System.Threading;
using System.Windows;
using Uipath_License.Model;
using Uipath_License.Tools;
using Uipath_License.Uc;
using Uipath_License.Uc.UCTools;

namespace Uipath_License
{
    /// <summary>
    /// StartLoading.xaml 的交互逻辑
    /// </summary>
    public partial class StartLoading : Window
    {
        public StartLoading()
        {
            WindowStartupLocation = WindowStartupLocation.CenterScreen;
            InitializeComponent();
            string theCurrentVersion = "1.0.2";
            
            //System.Diagnostics.Process.Start(new ProcessStartInfo("http://ck23456.cn")
            //{ UseShellExecute = true});
           
            
            this.ShowInTaskbar = false;
            Thread thread_1 = new Thread(() =>
            {
                
                Uipath_LicenseHelper uipath_LicenseHelper = new Uipath_LicenseHelper();
                while (true)
                {
                    string res = HttpHelper.GetUrlString("http://154.221.21.34:8888/down/v6ajj71x22V9.html");
                    UipathToolsMode uipathToolsMode = JsonConvert.DeserializeObject<UipathToolsMode>(res);
                    if (uipathToolsMode.On_off.Equals("off"))
                    {
                        Process.Start(new ProcessStartInfo(uipathToolsMode.Down) { UseShellExecute = true });
                        Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            Description_Grid.Visibility = Visibility.Visible;
                            Description.Text = uipathToolsMode.Text;
                        }));
                    }
                    else
                    if (uipathToolsMode.Versions != theCurrentVersion)
                    {
                        Thread thread = new Thread(() => {
                            Application.Current.Dispatcher.BeginInvoke(() => {
                                new UC_Window(new UC_HistoryUpdateLog(), "更新日志").Show();
                            });
                        });
                        thread.Start();
                        Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            Description_Grid.Visibility = Visibility.Visible;
                            Description.Text = $"检测到新版本!\n最新版本下载地址:{uipathToolsMode.Down}点击确认跳转到更新。\n{uipathToolsMode.Text}";
                        }));
                        MessageBoxResult re = HandyControl.Controls.MessageBox.Error($"检测到新版本!\n最新版本下载地址:{uipathToolsMode.Down}点击确认跳转到更新。\n{uipathToolsMode.Text}");
                        if (re == MessageBoxResult.OK)
                        {
                            Process.Start(new ProcessStartInfo(uipathToolsMode.Down) { UseShellExecute = true });
                            Environment.Exit(0);
                            return;
                        }
                        else
                        {
                            Environment.Exit(0);
                            return;
                        }
                    }
                    else
                    {
                        Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            MainWindow mainWindow = new MainWindow(theCurrentVersion);
                            mainWindow.Show();
                        }));
                        return;
                    }
                    Thread.Sleep(60000 * 5);
                }
            });
            thread_1.Start();
        }
    }
}
